package spring.example.cloud.service.impl;

import cloud.estate.commons.bean.Building;
import cloud.estate.commons.handler.exception.*;
import cloud.estate.commons.mapper.BuildingMapper;
import cloud.estate.commons.util.JsonResult;
import cloud.estate.commons.util.ResultTool;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import spring.example.cloud.service.BuildingService;

import java.util.List;

@Service
public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> implements BuildingService {
    @Override
    public JsonResult AddOneBuilding(Building building) {
        try {
            boolean save = save(building);
            if (save){
                return  ResultTool.success("添加成功");
            }
            return ResultTool.fail(501,"添加失败");
        } catch (Exception e) {
            throw new AddOneException(e.getMessage());
        }
    }
    @Override
    public JsonResult UpdateOneBuilding(Building building) {
        try {
            boolean update = updateById(building);
            if (update){
                return  ResultTool.success("修改成功");
            }
            return ResultTool.fail(501,"修改失败");
        } catch (Exception e) {
            throw new UpdateOneException(e.getMessage());
        }
    }
    @Override
    public JsonResult DeleteOneBuilding(String id) {
        try {
            boolean delete = removeById(id);
            if (delete){
                return  ResultTool.success("删除成功");
            }
            return ResultTool.fail(501,"删除失败");
        } catch (Exception e) {
            throw new DealOneByIdException(e.getMessage());
        }
    }
    @Override
    public JsonResult FindOneBuilding(String id) {
        try {
            Building building = getById(id);
            if (building != null){
                return  ResultTool.success(building);
            }
            return ResultTool.fail(501,"查询失败");
        } catch (Exception e) {
            throw new FindOneException(e.getMessage());
        }
    }
    @Override
    public JsonResult FindAllBuilding() {
        try {
            List<Building> list = list();
            if (list != null){
                return  ResultTool.success(list);
            }
            return ResultTool.fail(501,"查询失败");
        } catch (Exception e) {
            throw new FindAllException(e.getMessage());
        }

    }
}
